import React, { Component, PropTypes } from 'react';
import classNames from 'classnames';

import '../BodyDataInput.scss';
import LayoutView, { LayoutMode } from '../../layout';
import { SelectItemList, SelectItem } from '../../common-ui-parts';
import { BodyItemConstant as BiC } from '../../../constants/InputItemConstants';
import { BodyDataInputContextConstant } from '../../../constants/ComponentContextConstants';
import { MultipleValidationCategory as MvC } from '../../../validations/BodyDataInputValidation';

// 質問に関するイラスト
import imgQ9 from './images/imgQ9.png';

/**
 * Q9.血糖値
 */
class Question09 extends Component {
  getChildContext() {
    const { [BiC.BLOOD_SUGAR_LEVEL_FASTING]: bloodSugarLevelFasting } = this.context.parent.inputData;
    return Object.freeze({
      parent: Object.freeze(Object.assign({}, this.context.parent, {
        // すべて未入力の場合、[次へ]ボタンは無効
        disabledNext: [bloodSugarLevelFasting].every(input => input === undefined),
      })),
    });
  }

  shouldComponentUpdate(nextProps, nextState) {
    return this.context.parent.pageIndex === nextProps.index;
  }

  scrollPageTop() {
    this.refs.layoutView.getWrappedInstance().scrollTop();
  }

  validatePage() {
    // 入力チェック（true:チェックOK）
    return this.context.parent.validateQuestionPage([BiC.BLOOD_SUGAR_LEVEL_FASTING], [MvC.BLOOD_SUGAR_LEVEL]);
  }

  render() {
    const { inputData, errorInfo, updateState } = this.context.parent;

    return (
      <LayoutView
        ref="layoutView"
        mode={LayoutMode.QUESTION}
        title="血糖値"
        question={{
          no: 9,
          sentence: <p>空腹時の血糖をお答えください。<br/><br/></p>,
          illustSrc: imgQ9,
          illustHeight: 246,
        }}
        errorText={errorInfo.get(BiC.BLOOD_SUGAR_LEVEL_FASTING, MvC.BLOOD_SUGAR_LEVEL)}
      >
        {/* 空腹時 */}
        <dl className={classNames('content')}>
          <dt>{BiC.BLOOD_SUGAR_LEVEL_FASTING.label}</dt>
          <dd>
            <SelectItemList
              name={BiC.BLOOD_SUGAR_LEVEL_FASTING}
              value={inputData[BiC.BLOOD_SUGAR_LEVEL_FASTING]}
              placeholder="選択してください"
              propagate={updateState}
            >
              {BiC.BLOOD_SUGAR_LEVEL_FASTING.choices.map((choice, index) => (
                <SelectItem key={index} value={choice.value} label={choice.label} />
              ))}
            </SelectItemList>
          </dd>
        </dl>
      </LayoutView>
    );
  }

}

Question09.contextTypes = BodyDataInputContextConstant;
Question09.childContextTypes = Question09.contextTypes;

/*Question09.propTypes = {
  index: PropTypes.number,    // 自ページINDEX（0～）
};*/

export default Question09
